//
//  Collection_List_View.m
//  LoveTravel
//
//  Created by MS on 16-1-23.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "Collection_List_View.h"
#import "Collection_List_Cell.h"
#import "Collection_List_HeaderView.h"
#import "Collection_List_FooterView.h"
#import "UIImageView+WebCache.h"

#define ImageOffsetY -20
#define OffsetY 180
@interface Collection_List_View ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,weak) UITableView * tableView;
@property (nonatomic,weak) Collection_List_HeaderView * headerView;
@property (nonatomic,weak) Collection_List_FooterView * footerView;
@property (nonatomic,strong) UIImageView * headerImageView;
@end


@implementation Collection_List_View

-(Collection_List_HeaderView *)headerView
{
    if (_headerView==nil) {
        Collection_List_HeaderView * view =[Collection_List_HeaderView headerViewWithXib];
        _headerView = view;
        self.tableView.tableHeaderView = _headerView;
    }
    return _headerView;
}

-(Collection_List_FooterView *)footerView
{
    if (_footerView==nil) {
        Collection_List_FooterView * view = [Collection_List_FooterView footerViewWithXib];
        _footerView = view;
        self.tableView.tableFooterView = _footerView;
        
    }
    return _footerView;
}


-(void)setCollection_ListFrame:(CollecList_Frame *)collection_ListFrame
{
    _collection_ListFrame = collection_ListFrame;
    
    self.headerView.listFrame = collection_ListFrame;
    
    _headerView.frame = CGRectMake(0, 0, self.width, 20+25+5+collection_ListFrame.subtitleViewHeight+11+10+collection_ListFrame.textViewHeight+10);
    
    [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:collection_ListFrame.list.bg_pic[0]]];
    
    NSMutableAttributedString * atStr = [[NSMutableAttributedString alloc] initWithString:@"The End"];
    [atStr addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} range:NSMakeRange(4, 3)];
    self.footerView.titleView.attributedText = atStr;
    
    [_tableView reloadData];
}


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UITableView * tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        tableView.showsVerticalScrollIndicator = NO;
        self.tableView = tableView;
        [self addSubview:tableView];
       tableView.delegate = self;
        tableView.dataSource = self;
        tableView.rowHeight = UITableViewAutomaticDimension;
        tableView.estimatedRowHeight = 300;
        tableView.contentInset = UIEdgeInsetsMake(OffsetY, 0, 0, 0);
        
        tableView.backgroundColor = [UIColor clearColor];
        
        
        UIImageView * imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, ImageOffsetY, self.width, OffsetY+80)];
        imageV.contentMode = UIViewContentModeScaleAspectFill;
        imageV.clipsToBounds = YES;
        imageV.backgroundColor = [UIColor yellowColor];
        self.headerImageView = imageV;
        [self insertSubview:imageV belowSubview:tableView];
//        [tableView addSubview:imageV];
        
    }
    return self;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat offset =scrollView.contentOffset.y;
    
    /**
     *  发送标题显示/隐藏通知
     */
   BOOL titleHidden = offset>=0? NO : YES;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SetTitle" object:nil userInfo:@{@"Hidden":@(titleHidden)}];

    
    //下拉偏移量
    CGFloat offset_y =-scrollView.contentOffset.y-OffsetY;
  
    if (offset_y>=70) {
        self.tableView.contentOffset =CGPointMake(0, -OffsetY-70);
    }
    else{
         self.tableView.contentOffset = scrollView.contentOffset;
        self.headerImageView.y = ImageOffsetY+offset_y*0.35;
//        self.headerImageView.height = OffsetY+50+offset_y*0.35;
    }
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.collection_ListFrame.list.recomms.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Collection_List_Cell * cell = [Collection_List_Cell cellWithtableView:tableView];
    
    cell.recommData = self.collection_ListFrame.list.recomms[indexPath.row];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(collection_List_ViewDidCilck:recomm:index:)]) {
        [self.delegate collection_List_ViewDidCilck:self recomm:self.collection_ListFrame.list.recomms[indexPath.row] index:indexPath.row];
    }
    
}


@end
